package com.purong.purong.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.TimePickerView;
import com.purong.purong.Contant;
import com.purong.purong.R;
import com.purong.purong.bean.BankListBean;
import com.purong.purong.bean.TextItemBean;
import com.purong.purong.bean.UserPolicyBean;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.util.ZeroStartUnableTextWatch;
import com.purong.purong.widget.TextChoseDialog;
import com.yao.sdk.net.ResponseInfo;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zengwenjie on 2017/12/15.
 */
public class UserPolicyAddActivity extends BaseActivity {
    private EditText companyEd,insuredEd,payAgeEd,payMoneyed;
    private TextView startTimeTv,payTypeTv,policyTypeTv,countTv;
    private Button confirm;
    private TextChoseDialog mRegTimeDialog,mPayTypeDialog,mPolicyTypeDialog;
    private TextItemBean mRegTimeBean,mPayTypeBean,mPolicyTypeBean;
    private List<TextItemBean> itemBeanList=new ArrayList<>();
    private UserPolicyBean policyBean;
    @Override
    public void initBundle() {
        policyBean =(UserPolicyBean)getIntent().getSerializableExtra("USER_POLICY");
    }

    @Override
    public int getRootViewId() {
        return R.layout.activity_user_policy_add;
    }

    @Override
    public void initView() {
        showBack();
        showTitle("保单信息");
        countTv=(TextView)findViewById(R.id.count);
        companyEd=(EditText)findViewById(R.id.company_ed);
        insuredEd=(EditText)findViewById(R.id.insured_ed);
        payAgeEd=(EditText)findViewById(R.id.pay_age_ed);
        payMoneyed =(EditText)findViewById(R.id.pay_money_ed);
        payAgeEd.addTextChangedListener(new ZeroStartUnableTextWatch(payAgeEd));
        payMoneyed.addTextChangedListener(new ZeroStartUnableTextWatch(payMoneyed));
        startTimeTv=(TextView)findViewById(R.id.start_time_tv);
        payTypeTv=(TextView)findViewById(R.id.pay_type_tv);
        policyTypeTv=(TextView)findViewById(R.id.policy_type_tv);
        confirm=(Button)findViewById(R.id.confirm);
        if (policyBean ==null){
            confirm.setText("保存，进入下一保单");
        }else {
            confirm.setText("保存");
        }
    }
    private void addCredit(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        if (companyEd.getText().toString().trim().equals("")){
            showToast("保险公司为必填项");
            return;
        }else {
            params.put("company_name",companyEd.getText().toString().trim());
        }
        if (insuredEd.getText().toString().trim().equals("")){
            showToast("被保人为必填项");
            return;
        }else {
            params.put("insured_name",insuredEd.getText().toString().trim());
        }
        if (!startTimeTv.getText().equals("请选择")){
            params.put("start_time",startTimeTv.getText().toString().trim());
        }else {
            params.put("start_time","");
        }
        if (mPayTypeBean==null){
            showToast("缴费方式为必选项");
            return;
        }else {
            params.put("pay_type",mPayTypeBean.getId());
        }
        params.put("pay_age_limit",payAgeEd.getText().toString().trim());
        if (mPolicyTypeBean==null){
            showToast("保单类型为必选项");
            return;
        }else {
            params.put("policy_type",mPolicyTypeBean.getId());
        }
        params.put("pay_money",payMoneyed.getText().toString().trim());
        request(Contant.POLICY_ADD,params,true,true);
    }
    private void editCredit(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("user_policy_id",policyBean.getUser_policy_id());
        if (companyEd.getText().toString().trim().equals("")){
            showToast("保险公司为必填项");
            return;
        }else {
            params.put("company_name",companyEd.getText().toString().trim());
        }
        if (insuredEd.getText().toString().trim().equals("")){
            showToast("被保人为必填项");
            return;
        }else {
            params.put("insured_name",insuredEd.getText().toString().trim());
        }
        if (!startTimeTv.getText().equals("请选择")){
            params.put("start_time",startTimeTv.getText().toString().trim());
        }else {
            params.put("start_time","");
        }
        if (mPayTypeBean==null){
            showToast("缴费方式为必选项");
            return;
        }else {
            params.put("pay_type",mPayTypeBean.getId());
        }
        params.put("pay_age_limit",payAgeEd.getText().toString().trim());
        if (mPolicyTypeBean==null){
            showToast("保单类型为必选项");
            return;
        }else {
            params.put("policy_type",mPolicyTypeBean.getId());
        }
        params.put("pay_money",payMoneyed.getText().toString().trim());
        request(Contant.POLICY_EDIT,params,true,true);
    }

    private void setData(){
        if (policyBean ==null)
            return;
        if (policyBean.getCompany_name()!=null){
            companyEd.setText(policyBean.getCompany_name());
        }
        if (policyBean.getInsured_name()!=null){
            insuredEd.setText(policyBean.getInsured_name());
        }
        if (policyBean.getStart_time()!=null&&!policyBean.getStart_time().equals("")){
            startTimeTv.setText(policyBean.getStart_time());
            startTimeTv.setTextColor(getResources().getColor(R.color.blue));
        }
        if (policyBean.getPay_type()!=null){
            switch (policyBean.getPay_type()){
                case "1":
                    payTypeTv.setText("年缴");
                    payTypeTv.setTextColor(getResources().getColor(R.color.blue));
                    mPayTypeBean=new TextItemBean();
                    mPayTypeBean.setId("1");
                    mPayTypeBean.setName("年缴");
                    break;
                case "2":
                    payTypeTv.setText("月缴");
                    payTypeTv.setTextColor(getResources().getColor(R.color.blue));
                    mPayTypeBean=new TextItemBean();
                    mPayTypeBean.setId("2");
                    mPayTypeBean.setName("月缴");
                    break;
            }
        }
        if (policyBean.getPay_age_limit()!=null){
            payAgeEd.setText(policyBean.getPay_age_limit());
        }
        if (policyBean.getPolicy_type()!=null){
            switch (policyBean.getPolicy_type()){
                case "1":
                    policyTypeTv.setText("分红");
                    policyTypeTv.setTextColor(getResources().getColor(R.color.blue));
                    mPolicyTypeBean=new TextItemBean();
                    mPolicyTypeBean.setId("1");
                    mPolicyTypeBean.setName("分红");
                    break;
                case "2":
                    policyTypeTv.setText("寿险");
                    policyTypeTv.setTextColor(getResources().getColor(R.color.blue));
                    mPolicyTypeBean=new TextItemBean();
                    mPolicyTypeBean.setId("2");
                    mPolicyTypeBean.setName("寿险");
                    break;
                case "3":
                    policyTypeTv.setText("万能");
                    policyTypeTv.setTextColor(getResources().getColor(R.color.blue));
                    mPolicyTypeBean=new TextItemBean();
                    mPolicyTypeBean.setId("3");
                    mPolicyTypeBean.setName("万能");
                    break;
                case "4":
                    policyTypeTv.setText("其他");
                    policyTypeTv.setTextColor(getResources().getColor(R.color.blue));
                    mPolicyTypeBean=new TextItemBean();
                    mPolicyTypeBean.setId("4");
                    mPolicyTypeBean.setName("其他");
                    break;
            }
            if (policyBean.getPay_money()!=null){
                payMoneyed.setText(policyBean.getPay_money());
            }
        }

    }
    private void initPayTypeDialog(){
        mPayTypeDialog=new TextChoseDialog(this);
        mPayTypeDialog.setOnTextChoseListener(new TextChoseDialog.OnTextChoseListener() {
            @Override
            public void onChose(int position, TextItemBean item) {
                mPayTypeBean=item;
                payTypeTv.setText(mPayTypeBean.getName());
                payTypeTv.setTextColor(getResources().getColor(R.color.blue));
            }

        });
        List<TextItemBean> itemBeanList=new ArrayList<>();
        TextItemBean itemBean=new TextItemBean();
        itemBean.setId("1");
        itemBean.setName("年缴");
        itemBeanList.add(itemBean);
        itemBean=new TextItemBean();
        itemBean.setId("2");
        itemBean.setName("月缴");
        itemBeanList.add(itemBean);
        mPayTypeDialog.setData(itemBeanList);
    }
    private void initPolicyTypeDialog(){
        mPolicyTypeDialog=new TextChoseDialog(this);
        mPolicyTypeDialog.setOnTextChoseListener(new TextChoseDialog.OnTextChoseListener() {
            @Override
            public void onChose(int position, TextItemBean item) {
                mPolicyTypeBean=item;
                policyTypeTv.setText(mPolicyTypeBean.getName());
                policyTypeTv.setTextColor(getResources().getColor(R.color.blue));
            }

        });
        List<TextItemBean> itemBeanList=new ArrayList<>();
        TextItemBean itemBean=new TextItemBean();
        itemBean.setId("1");
        itemBean.setName("分红");
        itemBeanList.add(itemBean);
        itemBean=new TextItemBean();
        itemBean.setId("2");
        itemBean.setName("寿险");
        itemBeanList.add(itemBean);
        itemBean=new TextItemBean();
        itemBean.setId("3");
        itemBean.setName("万能");
        itemBeanList.add(itemBean);
        itemBean=new TextItemBean();
        itemBean.setId("4");
        itemBean.setName("其他");
        itemBeanList.add(itemBean);
        mPolicyTypeDialog.setData(itemBeanList);
    }
    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.POLICY_ADD)){
            if (responseInfo.isRequestOk){
                showToast("添加成功");
                Intent intent=new Intent(this,UserPolicyAddActivity.class);
                startActivity(intent);
                this.finish();
            }
        }else if (responseInfo.url.equals(Contant.POLICY_EDIT)){
            if (responseInfo.isRequestOk){
                showToast("编辑成功");
                this.finish();
            }
        }
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.policy_type_lay:
                if (mPolicyTypeDialog==null)
                    initPolicyTypeDialog();
                mPolicyTypeDialog.show();
                break;
            case R.id.pay_type_lay:
                if (mPayTypeDialog==null)
                    initPayTypeDialog();
                mPayTypeDialog.show();
                break;
            case R.id.start_time_lay:
                TimePickerView pvTime;
                pvTime = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
                //控制时间范围
                Calendar calendar = Calendar.getInstance();
                pvTime.setRange(1900, calendar.get(Calendar.YEAR));//要在setTime 之前才有效果

                if (startTimeTv.getText().equals("请选择")) {
                    pvTime.setTime(new Date());
                } else {
                    pvTime.setTime(CommonUtil.Date2Millis(startTimeTv.getText().toString()));
                }
                pvTime.setCyclic(true);
                pvTime.setCancelable(true);
                //时间选择后回调
                pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

                    @Override
                    public void onTimeSelect(Date date) {
                        startTimeTv.setText(CommonUtil.DateFormat(date));
                        startTimeTv.setTextColor(getResources().getColor(R.color.blue));
                    }
                });
                pvTime.show();
                break;
            case R.id.confirm:
                if (policyBean !=null){
                    editCredit();
                }else {
                    addCredit();
                }
                break;

        }
    }
    @Override
    public void initData() {
        if(policyBean !=null){
            setData();
        }
    }
}
